package com.start.workflow.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.entity.StartBaseEntity;
import com.start.common.entity.StartDicData;
import com.start.common.exceptions.StartBusException;
import com.start.common.tools.StartEnumTools;
import com.start.core.annotations.RegisterPermission;
import com.start.dataSource.entity.StartDataSource;
import com.start.dataSource.service.StartDataSourceService;
import com.start.form.engine.core.StartFormEngine;
import com.start.form.engine.core.entity.StartFormConfig;
import com.start.form.engine.core.service.StartFormConfigService;
import com.start.workflow.core.StartActFormType;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "流程表单管理")
@RestController
@RequestMapping("/start/actForm")
public class StartActFormController {
    @Autowired
    private StartFormConfigService startFormConfigService;
    @Autowired
    private StartDataSourceService sysDataSourceService;
    @Autowired
    private StartFormEngine formEngine;

    @GetMapping("/page")
    @PreAuthorize("@pas.hasPermission('start:act:form:info')")
    public Result<?> page(Page<StartFormConfig> page, StartFormConfig entity) {
        LambdaQueryWrapper<StartFormConfig> lambdaQuery = DataQueryConstructor.createEntity(entity).lambda();
        lambdaQuery.likeRight(StartFormConfig::getFormCategory, StartActFormType.START_ACT_FROM_PREFIX.getValue());
        lambdaQuery.orderByDesc(StartBaseEntity::getCreateTime);
        return Result.ok(startFormConfigService.page(page, lambdaQuery));
    }

    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('start:act:form:add')")
    @RegisterPermission(value = "start:act:form:add", name = "添加")
    public Result<?> add(@RequestBody StartFormConfig entity, boolean synchronization) {
        if (StringUtils.isAllBlank(entity.getFormCategory())) {
            throw new StartBusException("请选择表单类别");
        }
        formEngine.createForm(entity, synchronization);
        return Result.ok(entity.getId());
    }

    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('start:act:form:edit')")
    @RegisterPermission(value = "start:act:form:edit", name = "编辑")
    public Result<?> updateById(@RequestBody StartFormConfig entity, boolean synchronization) {
        if (StringUtils.isAllBlank(entity.getFormCategory())) {
            throw new StartBusException("请选择表单类别");
        }
        formEngine.updateForm(entity, synchronization);
        return Result.ok();
    }


    @GetMapping("/getById/{id}")
    @PreAuthorize("@pas.hasPermission('start:act:form:info')")
    @RegisterPermission(value = "start:act:form:info", name = "查看")
    public Result<?> getById(@PathVariable("id") String id) {
        return Result.ok(formEngine.getForm(id));
    }

    @GetMapping("/getByTableId/{id}")
    public Result<?> getByTableId(@PathVariable("id") String tableId) {
        return Result.ok(formEngine.getForm(tableId, true));
    }

    @GetMapping("/getTableOption/{tableId}")
    public Result<?> getTableOption(@PathVariable("tableId") String tableId) {
        return Result.ok(formEngine.getForm(tableId));
    }


    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('start:act:form:delete')")
    @RegisterPermission(value = "start:act:form:delete", name = "删除")
    public Result<?> deleteById(@PathVariable("id") String id) {
        formEngine.deleteForm(id);
        return Result.ok();
    }

    @PostMapping("/clearData/{tableId}")
    @RegisterPermission(value = "start:act:form:clearData", name = "清空数据")
    public Result<?> clearData(@PathVariable("tableId") String tableId) {
        formEngine.getTableEngineService(tableId).clearData(tableId);
        return Result.ok();
    }

    @GetMapping("/createMenu/{tableId}")
    @PreAuthorize("@pas.hasPermission('start:act:form:createMenu')")
    @RegisterPermission(value = "start:act:form:createMenu", name = "创建菜单")
    public Result<?> createMenu(@PathVariable("tableId") String tableId, String menuLabel, String menuParentId) {
        formEngine.getTableEngineService(tableId).createMenu(tableId, menuLabel, menuParentId);
        return Result.ok();
    }

    @GetMapping("/synchronizationTable/{tableId}/{version}")
    @PreAuthorize("@pas.hasPermission('start:act:form:synchronizationTable')")
    @RegisterPermission(value = "start:act:form:synchronizationTable", name = "同步或更新数据表")
    public Result<?> synchronizationTable(@PathVariable("tableId") String tableId, @PathVariable("version") Integer version) {
        formEngine.getTableEngineService(tableId).synchronizationTable(tableId);
        return Result.ok();
    }

    @GetMapping("/getAllDataSource")
    @PreAuthorize("@pas.hasPermission('start:act:form:getAllDataSource')")
    @RegisterPermission(value = "start:act:form:getAllDataSource", name = "获取全部数据源")
    public Result<?> getAllDataSource() {
        QueryWrapper<StartDataSource> queryWrapper = DataQueryConstructor.createEntity(StartDataSource.class);
        return Result.ok(sysDataSourceService.list(queryWrapper));
    }

    @GetMapping("/getActFormCategory")
    public Result<?> getActFormCategory() {
        List<StartDicData> enumDic = StartEnumTools.getEnumDic(StartActFormType.class);
        return Result.ok(enumDic);
    }

    @GetMapping("/getAllStorage")
    public Result<?> getAllStorage() {
        return Result.ok(formEngine.getAllStorage());
    }

}
